package org.example.springboot.service;

import org.example.springboot.entity.News;
import org.example.springboot.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NewsService {

    @Autowired
    private NewsMapper newsMapper;

    public List<News> getNewsByPage(int pageSize, int pageNumber, String title) {
        int offset = (pageNumber - 1) * pageSize;
        return newsMapper.getNewsByPage(pageSize, offset, title);
    }


    public int getNewsCount(String title) {
        return newsMapper.getNewsCount(title);
    }

    @Transactional
    public void deleteNewsById(int id) {
        newsMapper.deleteNewsById(id);
    }

    @Transactional
    public void insertNews(News news) {
        newsMapper.insertNews(news);
    }


    public News getNewsById(int id) {
        return newsMapper.selectById(id);
    }

    @Transactional
    public void updateNews(News news) {
        newsMapper.updateNews(news);
    }

    public List<News> getAllNews() {
        return newsMapper.selectAll();
    }
}
